﻿//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
// Exo² - Tools - Studio/ExefBuilderModule
// Copyright (C) 2007-2008 Samuel Lacroix and Raphaël Legouge
//
// Distributed under GNU General Public License
// See the License folder for details
// http://www.codeplex.com/exo2 (eng)
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace ExefBuilderModule
{
    public partial class ExefBuilder : BasicModule.MainForm
    {
        public ExefBuilder()
        {
            InitializeComponent();

            this.createPhysiology = new CreatePhysiology();
            this.chooseDefaultPhysiology = new ChooseDefaultPhysiology();
            this.physiologyGeneration = new PhysiologyGeneration();
            
            this.physiologyCreationData = new Dictionary<string,CPhysiologyCreationData>();
        }

        private CreatePhysiology createPhysiology;
        private ChooseDefaultPhysiology chooseDefaultPhysiology;
        private PhysiologyGeneration physiologyGeneration;
        
        private Dictionary<string, CPhysiologyCreationData> physiologyCreationData;
        private string defaultPhysiologyName;

        private void button3_Click(object sender, EventArgs e)
        {
                this.createPhysiology.ResetForm();
                DialogResult DR = this.createPhysiology.ShowDialog();
                if (DR == DialogResult.OK)
                {
                        string physiologyName = this.createPhysiology.getPhysiologyName();
                        if (this.physiologyCreationData.ContainsKey(physiologyName))
                        {
                                MessageBox.Show("this physiology already exists.");
                                return;
                        }
                        CPhysiologyCreationData Data = new CPhysiologyCreationData();
                        Data.massOrDensity = this.createPhysiology.getMassOrDensity();
                        Data.physiologyName = physiologyName;
                        Data.sourceXFile = this.createPhysiology.getSourceXFileName();
                        Data.targetXFile = (this.createPhysiology.getSaveTargetXFile()) ? this.createPhysiology.getTargetXFileName() : "";
                        Data.useMass = this.createPhysiology.getUseMass();
                        this.physiologyCreationData.Add(physiologyName, Data);
                        this.listBox1.Items.Add(physiologyName);
                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                if (this.listBox1.SelectedItem != null)
                {
                        if (MessageBox.Show("are you sure to delete " + this.listBox1.SelectedItem.ToString() + " ?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                                this.physiologyCreationData.Remove(this.listBox1.SelectedItem.ToString());
                                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
                        }
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                if (this.listBox1.Items.Count > 0)
                {
                        if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                                string exefName = this.saveFileDialog1.FileName;
                                string[] physiologyNames = new string[this.listBox1.Items.Count];
                                int i = 0;
                                foreach (string name in this.listBox1.Items)
                                    physiologyNames.SetValue(name, i++);
                                this.chooseDefaultPhysiology.ResetForm(physiologyNames);
                                if (this.chooseDefaultPhysiology.ShowDialog() == DialogResult.OK)
                                {
                                        this.defaultPhysiologyName = this.chooseDefaultPhysiology.getDefaultPhysiologyName();
                                        
                                        foreach (CPhysiologyCreationData data in this.physiologyCreationData.Values)
                                        {
                                                this.physiologyGeneration.ResetForm(data.physiologyName, data.massOrDensity, data.useMass, data.sourceXFile, data.targetXFile != "", data.targetXFile);
                                                this.physiologyGeneration.CreatePhysiology();
                                                if (this.physiologyGeneration.ShowDialog() == DialogResult.OK)
                                                {
                                        
                                                }
                                        }
                                }
                        }
                }
        }
    }
    
    public class CPhysiologyCreationData
    {
        public double massOrDensity;
        public Boolean useMass;
        public string sourceXFile;
        public string targetXFile;
        public string physiologyName;
    }
}
